package com.dearbinge.opentapi.content.orderTable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dearbinge.data.api.orderDetailTable.OrderDetailTableService;
import com.dearbinge.data.api.orderTable.OrderTableService;
import com.dearbinge.data.pojo.orderTable.OrderTable;
import com.dearbinge.utils.PageResult;
import com.dearbinge.utils.ResultMsg;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Api(description = "订单")
@RestController
@CrossOrigin
@RequestMapping("/orderTable")
@EnableAutoConfiguration
public class OrderTableController extends WebMvcConfigurerAdapter {

	@Autowired
	private OrderTableService orderTableService;

	@Autowired
	private OrderDetailTableService orderDetailTableService;

	/**
	 * 根据微信订单编号修改订单地址
	 */
	@ApiOperation(value = "根据微信订单编号修改订单地址", notes = "根据微信订单编号修改订单地址")
	@RequestMapping(value = "/updateOrderByWechatNumber", method = RequestMethod.POST)
	public ResultMsg<String> updateOrderByWechatNumber(@RequestBody OrderTable orderTable ) {
		try {
			Integer dataCode = orderTableService.update(orderTable);
			if (dataCode > 0) {
				return ResultMsg.ok("修改成功");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultMsg.error(500, "参数为空");
	}


//	{
//		"data": [
//			[{
//					"shopId": "95",
//					"userId": "901"
//				},
//				[{
//					"modifyBy": "901",
//					"transactionPrice": 45,
//					"commoditySpecName": "炫白薄荷型",
//					"commodityId": "537",
//					"source": 1,
//					"userId": "901",
//					"product_name": "德国汉高原装进口施华蔻超感亮白薄荷立式牙膏100g真空按压正品",
//					"imgUrl": "https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908210.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908211.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908372.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908373.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908374.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908385.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908386.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908387.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181908398.png",
//					"commoditySpecId": "791",
//					"total": 1,
//					"id": 355,
//					"shopId": 95,
//					"commodityName": "德国汉高原装进口施华蔻超感亮白薄荷立式牙膏100g真空按压正品"
//				},
//				{
//					"modifyBy": "901",
//					"transactionPrice": 119,
//					"commoditySpecName": "人参滋养双膏",
//					"commodityId": "535",
//					"source": 1,
//					"userId": "901",
//					"product_name": "施华蔻怡然滋养染发霜 草本染发膏 人参胶原滋养双膏组批发正品",
//					"imgUrl": "https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904490.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904501.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904512.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904513.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904514.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904525.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904526.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904527.png,https://uwz.blob.core.chinacloudapi.cn/quickstartcontainer/928201903181904538.png",
//					"commoditySpecId": "786",
//					"total": 1,
//					"id": 356,
//					"shopId": 95,
//					"commodityName": "施华蔻怡然滋养染发霜 草本染发膏 人参胶原滋养双膏组批发正品"
//				}]
//			]
//		]
//	}
	/**
	 * 新增/修改
	 *
	 */
	@ApiOperation(value = "新增", notes = "新增")
	@RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
	public ResultMsg<String> addOrUpdate(@RequestBody String listJSON) {
		try {
			JSONObject parseObject = JSON.parseObject(listJSON);
			System.out.println("parseObject==="+parseObject);
			String addOrUpdate = orderTableService.addOrUpdate(parseObject);
			return ResultMsg.ok(addOrUpdate);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResultMsg.error(500, "系统异常");
	}




//	/**
//	 * 根据ID删除
//	 */
//	@ApiOperation(value = "根据ID删除", notes = "根据ID删除")
//	@ApiImplicitParam(name = "id", value = "id", paramType = "query")
//	@RequestMapping(value = "/delete", method = RequestMethod.GET)
//	public ResultMsg<String> delete(Integer id) {
//		try {
//			Integer dataCode = orderTableService.delete(id);
//			if (dataCode > 0) {
//				return ResultMsg.ok("删除成功");
//			}
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		return ResultMsg.error(500, "系统异常");
//	}

	/**
	 * 根据条件分页查询
	 */
	@ApiOperation(value = "根据条件分页查询", notes = "根据条件分页查询")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId", value = "用户ID",paramType = "query"),
			@ApiImplicitParam(name = "orderStatus", value = "订单状态： 0 待付款 1 待发货 2 待收货 3 待评价4失效订单5.退款7.失效订单",paramType = "query"),
			@ApiImplicitParam(name = "assessType", value = "是否评价（0：已评价  1：未评价）",paramType = "query"),
			@ApiImplicitParam(name = "orderId", value = "订单id",paramType = "query"),
			@ApiImplicitParam(name = "isDeleted", value = "全部:1，其余选项传：0",paramType = "query"),
			@ApiImplicitParam(name = "type", value = "APP调用:1,html5调用:2",paramType = "query"),
			@ApiImplicitParam(name = "userType", value = "卖家1买家2",paramType = "query"),
			@ApiImplicitParam(name = "messageSpecific", value = "消息页面专用必传1",paramType = "query"),
			@ApiImplicitParam(name = "pageNo", value = "当前页",paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "页面数量",paramType = "query") })
	@RequestMapping(value = "/getPage", method = RequestMethod.GET)
	public PageResult<List<Object>> getPage(String messageSpecific,String userType,String type,String orderId,String userId,Integer orderStatus,Integer assessType,Integer isDeleted, Integer pageNo, Integer pageSize) {
		PageResult<List<Object>> pageResult = new PageResult<>();
		try {
			pageResult = orderTableService.getPage(messageSpecific ,userType,type,orderId,userId,orderStatus, assessType,isDeleted,pageNo, pageSize);
		} catch (Exception e) {
			pageResult.setStatusCode(500);
			e.printStackTrace();
		}
		pageResult.setStatusCode(200);
		return pageResult;
	}

//	/**
//	 * 根据订单编号查询订单详情
//	 */
//	@ApiOperation(value = "根据订单编号查询订单详情",notes = "根据订单编号查询订单详情")
//    @ApiImplicitParam(name = "orderNum",value = "订单编号",paramType = "query")
//    @RequestMapping(value = "/getByUserId",method = RequestMethod.GET)
//    public ResultMsg<List<Object>> getByUserId(String orderNum){
//	    try {
//	        List<Object> list = orderTableService.getByUserId(orderNum);
//	        return ResultMsg.ok(list);
//        }catch (Exception e){
//	        e.printStackTrace();
//        }
//	    return ResultMsg.error(500,"系统异常");
//    }

    /**
     * 支付成功之后修改订单状态
     */
    @ApiOperation(value = "支付成功之后修改订单状态",notes = "支付成功之后修改订单状态")
    @ApiImplicitParams({@ApiImplicitParam(name = "orderNum",value = "订单编号",paramType = "query"),
            @ApiImplicitParam(name = "orderStatus",value = "订单状态： 0 待付款 1 待发货 2 待收货 3 待评价4失效订单5.退款",paramType = "query")})
    @RequestMapping(value = "/updateStatus",method = RequestMethod.GET)
    public ResultMsg<String> updateStatus(String orderNum,Integer orderStatus){
        try {
            Integer dataCode = orderTableService.updateStatus(orderNum,orderStatus);
            if (dataCode > 0){
                return ResultMsg.ok("支付成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }

    /**
     * 确认发货
     */
    @ApiOperation(value = "确认发货",notes = "确认发货")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "expressNum",value = "快递单号",paramType = "query"),
    	@ApiImplicitParam(name = "orderNum",value = "订单编号",paramType = "query"),
    	@ApiImplicitParam(name = "expressName",value = "快递公司名字",paramType = "query"),
    	@ApiImplicitParam(name = "orderCompanyCode",value = "快递公司编码",paramType = "query"),
    	@ApiImplicitParam(name = "orderStatus",value = "订单状态：  2 ",paramType = "query")
    })
    @RequestMapping(value = "/deliverGoods",method = RequestMethod.GET)
    public ResultMsg<String> deliverGoods(String expressNum,String orderNum,String orderCompanyCode,String orderStatus,String expressName){
        try {
            Integer dataCode = orderTableService.deliverGoods(expressNum,orderNum,orderCompanyCode,orderStatus,expressName);
            if (dataCode > 0){
                return ResultMsg.ok("确认发货成功");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }


    @ApiOperation(value = "根据订单编号查询要评价的商品",notes = "根据订单编号查询要评价的商品")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "orderNum",value = "订单编号",paramType = "query"),
    })
    @RequestMapping(value = "/getCommodityByOrderNo",method = RequestMethod.GET)
    public ResultMsg<Map<String, String>> getCommodityByOrderNo(String orderNum){
        try {
            Map<String, String> commodityByOrderNo = orderTableService.getCommodityByOrderNo(orderNum);
            return ResultMsg.ok(commodityByOrderNo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return ResultMsg.error(500,"系统异常");
    }






    @ApiOperation(value = "根据订卖家id查询待评价的商品",notes = "根据订卖家id查询待评价的商品")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "userId",value = "用户id",paramType = "query"),
    	@ApiImplicitParam(name = "pageNo", value = "当前页",paramType = "query"),
		@ApiImplicitParam(name = "pageSize", value = "页面数量",paramType = "query")
    })
    @RequestMapping(value = "/getCommodityByUserId",method = RequestMethod.GET)
    public PageResult<List<Object>> getCommodityByUserId(String userId , Integer pageNo, Integer pageSize){
    	PageResult<List<Object>> pageResult = new PageResult<>();
    	try {
        	pageResult = orderTableService.getCommodityByUserId(userId ,pageNo, pageSize);
    	} catch (Exception e) {
			pageResult.setStatusCode(500);
			e.printStackTrace();
		}
		pageResult.setStatusCode(200);
		return pageResult;
    }


}
